package org.slsale.controller;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.slsale.pojo.User;
import org.slsale.service.user.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController extends BaseController {
	private Logger logger = Logger.getLogger(UserController.class);
	
	@Resource
	private UserService userService;

	/**
	 * 密码修改
	 * @param userJson
	 * @return
	 * /backend/modifyPwd.html
	 */
	@RequestMapping(value = "/backend/modifyPwd.html")
	@ResponseBody
	public Object modifyPwd(@RequestParam String userJson){
		logger.debug("modifyPwd ===================== ");
		//获取当前用户
		User sessionUser = this.getCurrentUser();
		if(userJson == null || userJson.equals("")){
			return "nodata";
		}else{
			//把json数据转换成user集合 ---把前端密码password-->存入user集合
			JSONObject userObject = JSONObject.fromObject(userJson);
			User user = (User)JSONObject.toBean(userObject,User.class);

			user.setId(sessionUser.getId());
			user.setLoginCode(sessionUser.getLoginCode());
			try {
				if(userService.getLoginUser(user)!=null){
					user.setPassword(user.getPassword2());
					user.setPassword2(null);
					userService.modifyUser(user);
				}else{
					return "oldpwdwrong";
				}
			} catch (Exception e) {
				return "failed";
			}
		}
		return "success";
	}
}
